package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.HospitalSubscribeScheduleDays;
import com.ruoyi.system.service.IHospitalSubscribeScheduleDaysService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.HospitalSubscribeScheduleCapacityMapper;
import com.ruoyi.system.domain.HospitalSubscribeScheduleCapacity;
import com.ruoyi.system.service.IHospitalSubscribeScheduleCapacityService;

/**
 * 时段余量Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-04-28
 */
@Service
public class HospitalSubscribeScheduleCapacityServiceImpl implements IHospitalSubscribeScheduleCapacityService 
{
    @Autowired
    private HospitalSubscribeScheduleCapacityMapper hospitalSubscribeScheduleCapacityMapper;

    @Autowired
    private IHospitalSubscribeScheduleDaysService iHospitalSubscribeScheduleDaysService;

    /**
     * 查询时段余量
     * 
     * @param id 时段余量主键
     * @return 时段余量
     */
    @Override
    public HospitalSubscribeScheduleCapacity selectHospitalSubscribeScheduleCapacityById(Long id)
    {
        return hospitalSubscribeScheduleCapacityMapper.selectHospitalSubscribeScheduleCapacityById(id);
    }

    /**
     * 查询时段余量列表
     * 
     * @param hospitalSubscribeScheduleCapacity 时段余量
     * @return 时段余量
     */
    @Override
    public List<HospitalSubscribeScheduleCapacity> selectHospitalSubscribeScheduleCapacityList(HospitalSubscribeScheduleCapacity hospitalSubscribeScheduleCapacity)
    {
        return hospitalSubscribeScheduleCapacityMapper.selectHospitalSubscribeScheduleCapacityList(hospitalSubscribeScheduleCapacity);
    }

    /**
     * 新增时段余量
     * 
     * @param hospitalSubscribeScheduleCapacity 时段余量
     * @return 结果
     */
    @Override
    public int insertHospitalSubscribeScheduleCapacity(HospitalSubscribeScheduleCapacity hospitalSubscribeScheduleCapacity)
    {
        if (hospitalSubscribeScheduleCapacity.getDays() != null){
            HospitalSubscribeScheduleDays scheduleDays = new HospitalSubscribeScheduleDays();
            scheduleDays.setDate(hospitalSubscribeScheduleCapacity.getDays());
            iHospitalSubscribeScheduleDaysService.insertHospitalSubscribeScheduleDays(scheduleDays);
            hospitalSubscribeScheduleCapacity.setDaysId(scheduleDays.getId());
        }
        hospitalSubscribeScheduleCapacity.setCreateTime(DateUtils.getNowDate());
        return hospitalSubscribeScheduleCapacityMapper.insertHospitalSubscribeScheduleCapacity(hospitalSubscribeScheduleCapacity);
    }

    /**
     * 修改时段余量
     * 
     * @param hospitalSubscribeScheduleCapacity 时段余量
     * @return 结果
     */
    @Override
    public int updateHospitalSubscribeScheduleCapacity(HospitalSubscribeScheduleCapacity hospitalSubscribeScheduleCapacity)
    {
        hospitalSubscribeScheduleCapacity.setUpdateTime(DateUtils.getNowDate());
        return hospitalSubscribeScheduleCapacityMapper.updateHospitalSubscribeScheduleCapacity(hospitalSubscribeScheduleCapacity);
    }

    /**
     * 批量删除时段余量
     * 
     * @param ids 需要删除的时段余量主键
     * @return 结果
     */
    @Override
    public int deleteHospitalSubscribeScheduleCapacityByIds(Long[] ids)
    {
        return hospitalSubscribeScheduleCapacityMapper.deleteHospitalSubscribeScheduleCapacityByIds(ids);
    }

    /**
     * 删除时段余量信息
     * 
     * @param id 时段余量主键
     * @return 结果
     */
    @Override
    public int deleteHospitalSubscribeScheduleCapacityById(Long id)
    {
        return hospitalSubscribeScheduleCapacityMapper.deleteHospitalSubscribeScheduleCapacityById(id);
    }

    /**
     * 扣减时间段余量
     * @param scheduleCapacityId
     */
    @Override
    public int reduceNumberByScheduleCapacityId(Long scheduleCapacityId) {
        return hospitalSubscribeScheduleCapacityMapper.reduceNumberByScheduleCapacityId(scheduleCapacityId);
    }

    /**
     * 根据时间段id恢复余量
     * @param scheduleCapacityId
     * @return
     */
    @Override
    public int updateAvailableSlotsNumberById(Long scheduleCapacityId) {
        return hospitalSubscribeScheduleCapacityMapper.updateAvailableSlotsNumberById(scheduleCapacityId);
    }
}
